package com.sqx.modules.creation.controller.app;

import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.creation.entity.dto.SelCreationListDto;
import com.sqx.modules.creation.entity.dto.UserWriteDto;
import com.sqx.modules.creation.service.CreationService;
import com.sqx.modules.creation.service.LabelService;
import io.swagger.annotations.ApiParam;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import net.dreamlu.mica.ip2region.core.IpInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app/creation")
public class AppCreationController {
    @Autowired
    private CreationService creationService;
    @Autowired
    private Ip2regionSearcher ip2regionSearcher;
    /**
     * 分页
     * 社区分类查询作品
     * @param
     * @return
     */
    @Login
    @GetMapping("/selCreationList")
    public Result selCreationList(@RequestAttribute("userId")Long userId, SelCreationListDto selCreationListDto,HttpServletRequest request) {
        String ip = getIp(request);
        IpInfo ipInfo = ip2regionSearcher.memorySearch(ip);
        String city = ipInfo.getCity();
        selCreationListDto.setCity(city);
        return creationService.selCreationList(userId,selCreationListDto);
    }

    /**
     * 不分页查询标签列表
     * @return
     */
    @GetMapping("/selLabels")
    public Result selLabels(){
        return creationService.selLabels();
    }

    /**
     * 创作
     * @param userWriteDto
     * @return
     */
    @Login
    @PostMapping("/userWrite")
    public Result userWrite(@RequestAttribute("userId")Long userId, @RequestBody UserWriteDto userWriteDto,HttpServletRequest request){
        String ip = getIp(request);
        IpInfo ipInfo = ip2regionSearcher.memorySearch(ip);
        String city = ipInfo.getCity();
        userWriteDto.setCreationCity(city);
        return creationService.userWrite(userId,userWriteDto);
    }

    /**
     * 我的主页创作查询
     * @param userId
     * @param page
     * @param limit
     * @return
     */
    @Login
    @GetMapping("/getMyCreationList")
    public Result getMyCreationList(@RequestAttribute("userId")Long userId, Integer page, Integer limit, Long selUserId,Long classify){
        return creationService.getMyCreationList(userId,page,limit,selUserId,classify);
    }

    /**
     * 我的关注
     * @param userId
     * @param page
     * @param limit
     * @return
     */
    @Login
    @GetMapping("/getMyFollowList")
    public Result getMyFollowList(@RequestAttribute("userId")Long userId, Integer page, Integer limit){
        return creationService.getMyFollowList(userId,page,limit);
    }

    /***
     * 我的粉丝
     * @param userId
     * @param page
     * @param limit
     * @return
     */
    @Login
    @GetMapping("/getMyFansList")
    public Result getMyFansList(@RequestAttribute("userId")Long userId, Integer page, Integer limit){
        return creationService.getMyFansList(userId,page,limit);
    }

    /**
     * 删除文章
     * @param creationId
     * @return
     */
    @Login
    @PostMapping("/deleteCreation/{creationId}")
    public Result deleteCreation(@RequestAttribute("userId")Long userId,@PathVariable Long creationId){
        return creationService.deleteCreation(userId,creationId);
    }

    /**
     * 根据创作id查询作品详情
     * @param userId
     * @param creationId
     * @return
     */
    @Login
    @GetMapping("/selCreationById")
    public Result selCreationById(@RequestAttribute("userId")Long userId,Long creationId){
        return creationService.selCreationById(userId,creationId);
    }

    /**
     * 用户主页查询信息
     * @param userId
     * @return
     */
    @Login
    @GetMapping("/selUserInfo")
    public Result selUserInfo(@RequestAttribute("userId")Long userId,Long selUserId){
        return creationService.selUserInfo(userId,selUserId);
    }

    /**
     * 从请求头获取用户IP
     * @param request
     * @return
     */
    private String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            if (ip!= null && ip.length() > 15 && ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        return ip;
    }

}
